package com.acc.softmgr.business.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.acc.softmgr.business.model.SoftmgrProject;
import com.acc.softmgr.business.model.SoftmgrProjectExample;
import com.acc.softmgr.business.model.SysUser;
import com.acc.softmgr.business.service.ISoftmgrProjectService;
import com.acc.softmgr.common.constant.DeleteStatus;
import com.acc.softmgr.common.constant.enums.RestStatus;
import com.acc.softmgr.common.utils.date.DateBase;
import com.acc.softmgr.common.utils.string.StringUtil;
import com.acc.softmgr.common.web.DataGridObject;
import com.acc.softmgr.common.web.ResponseObject;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @content
 * 
 * @date 2017年5月8日
 * 
 * @author OprCalf
 * 
 */
@Controller
@RequestMapping("softProject")
public class SoftmgrProjectController {

	@Autowired
	private ISoftmgrProjectService softmgrProjectService;

	@RequestMapping(value = "toSoftProject", method = RequestMethod.GET)
	public String toSoftProject() {
		return "softProject/softProject";
	}

	/**
	 * 查询列表页
	 * 
	 * @param project_name
	 * @param project_english
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "searchSoftProject", method = RequestMethod.GET)
	@ResponseBody
	public DataGridObject<SoftmgrProject> searchSoftProject(@Validated String project_name,
			@Validated String project_english, Integer page, Integer rows, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DataGridObject<SoftmgrProject> responseObject = new DataGridObject<SoftmgrProject>();
		SoftmgrProjectExample example = new SoftmgrProjectExample();
		if (!StringUtil.isEmpty(project_name)) {
			example.getCriteria().andProject_nameLike("%" + project_name + "%");
		}
		if (page == null || page == 0) {
			page = 1;
		}

		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		example.setCount(page * rows);
		example.setLimitStart((page - 1) * rows);

		List<SoftmgrProject> softmgrProjects = softmgrProjectService.selectByExample(example);
		for (SoftmgrProject softmgrProject : softmgrProjects) {
			softmgrProject.setCreateTimestring(
					DateBase.formatDate(softmgrProject.getCreatetime(), DateBase.DATE_PATTERN_DATETIME));
		}
		responseObject.setTotal(page * rows);
		responseObject.setRows(softmgrProjects);
		return responseObject;
	}

	/**
	 * 新增项目
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addSoftProject", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject addSoftProject(@Validated String project_name, String project_english,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (StringUtil.isEmpty(project_name) || StringUtil.isEmpty(project_english)) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SysUser currentUser = (SysUser) request.getSession().getAttribute("currentUser");

		SoftmgrProject SoftmgrProject = new SoftmgrProject();
		SoftmgrProject.setCreatetime(new Date());
		SoftmgrProject.setCreateby(currentUser.getUsername());
		SoftmgrProject.setProject_english(project_english);
		SoftmgrProject.setProject_name(project_name);
		SoftmgrProject.setDeleted(DeleteStatus.FALSE);
		// -------------------
		SoftmgrProjectExample example = new SoftmgrProjectExample();
		example.getCriteria().andProject_nameEqualTo(project_name);
		example.getCriteria().andProject_englishEqualTo(project_english);
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		List<SoftmgrProject> softmgrProjects = softmgrProjectService.selectByExample(example);
		if (softmgrProjects != null && softmgrProjects.size() > 0) {
			responseObject.setStatus(RestStatus.NOT_UNIQUE.code);
			responseObject.setMessage(RestStatus.NOT_UNIQUE.message);
			return responseObject;
		}
		// ----------判断唯一性------------
		softmgrProjectService.insert(SoftmgrProject);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 修改用户
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "editSoftProject", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject editSoftProject(@Validated Integer id, String project_name, String project_english,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (StringUtil.isEmpty(project_name) || StringUtil.isEmpty(project_english) || id == null || id == 0) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SoftmgrProject softmgrProject = softmgrProjectService.selectByPrimaryKey(id);
		boolean exitSoftProject = exitSoftmgrProject(project_name, project_english);
		if (exitSoftProject) {
			responseObject.setStatus(RestStatus.NOT_UNIQUE.code);
			responseObject.setMessage(RestStatus.NOT_UNIQUE.message);
			return responseObject;
		}
		// ----------判断唯一性------------
		softmgrProject.setProject_name(project_name);
		softmgrProject.setProject_english(project_english);
		softmgrProjectService.updateByPrimaryKey(softmgrProject);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 查询详情
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/{id}/searchSoftProject", method = RequestMethod.GET)
	@ResponseBody
	public ResponseObject searchSoftProject(@PathVariable Integer id) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (id == null || id == 0) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SoftmgrProject SoftmgrProject = softmgrProjectService.selectByPrimaryKey(id);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		responseObject.setData(SoftmgrProject);
		return responseObject;
	}

	/**
	 * 删除项目
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/removeSoftProject", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObject removeSoftProject(@Validated Integer id) throws Exception {
		ResponseObject responseObject = new ResponseObject();
		if (id == null || id == 0) {
			responseObject.setStatus(RestStatus.PARAMETER_REQUIRED.code);
			responseObject.setMessage(RestStatus.PARAMETER_REQUIRED.message);
			return responseObject;
		}
		SoftmgrProject softmgrProject = softmgrProjectService.selectByPrimaryKey(id);
		softmgrProject.setDeleted(DeleteStatus.TRUE);
		softmgrProjectService.updateByPrimaryKey(softmgrProject);
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	/**
	 * 获取所有project项目
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "getSoftProjects", method = RequestMethod.GET)
	@ResponseBody
	public String getSoftProjects() throws Exception {

		SoftmgrProjectExample example = new SoftmgrProjectExample();
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);

		List<SoftmgrProject> list = softmgrProjectService.selectByExample(example);
		JSONArray jsonArray = new JSONArray();
		for (SoftmgrProject project : list) {
			JSONObject json = new JSONObject();
			json.put("id", project.getId());
			json.put("text", project.getProject_name());
			jsonArray.add(json);
		}

		return jsonArray.toJSONString();
	}

	private boolean exitSoftmgrProject(String project_name, String project_english) {
		SoftmgrProjectExample example = new SoftmgrProjectExample();
		example.getCriteria().andProject_nameEqualTo(project_name);
		example.getCriteria().andProject_englishEqualTo(project_english);
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		List<SoftmgrProject> softmgrProjects = softmgrProjectService.selectByExample(example);
		if (softmgrProjects != null && softmgrProjects.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

}
